package com.test.base.service;

import com.test.base.dao.NideshopCartDao;
import com.test.base.dao.NideshopGoodsDao;
import com.test.base.entity.NideshopCart;
import com.test.base.entity.NideshopGoods;
import com.test.base.utils.Assist;
import com.test.base.vo.api.CartTotal;
import com.test.base.vo.api.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车
 */
@Service
public class CartService {
    @Autowired
    private NideshopCartDao nideshopCartDao;

    @Autowired
    private NideshopGoodsDao nideshopGoodsDao;

    /**
     * 获取购物车的商品
     *
     * @returns
     */
    public List<NideshopCart> getGoodsList(Long userId) {
        Assist cart = new Assist();
        cart.setRequires(cart.andEq("user_id", userId));
        cart.setRequires(cart.andEq("session_id", 1));
        List<NideshopCart> result = nideshopCartDao.selectNideshopCart(cart);
        return result;
    }

    /**
     * 获取购物车的选中的商品
     *
     * @returns
     */
    public List<NideshopCart> getCheckedGoodsList(Long userId) {
        Assist cart = new Assist();
        cart.setRequires(cart.andEq("user_id", userId));
        cart.setRequires(cart.andEq("session_id", 1));
        cart.setRequires(cart.andEq("checked", 1));
        List<NideshopCart> result = nideshopCartDao.selectNideshopCart(cart);
        return result;
    }

    /**
     * 清空已购买的商品
     *
     * @returns
     */
    public boolean clearBuyGoods(Long userId) {
        Assist cart = new Assist();
        cart.setRequires(cart.andEq("user_id", userId));
        cart.setRequires(cart.andEq("session_id", 1));
        cart.setRequires(cart.andEq("checked", 1));
        int result = nideshopCartDao.deleteNideshopCart(cart);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }


   public CartVo getCart(Long userId){
       CartVo cartVo = new CartVo();
       int goodsCount = 0;
       double goodsAmount = 0.00;
       int checkedGoodsCount = 0;
       double checkedGoodsAmount = 0.00;
       Assist cart = new Assist();
       cart.setRequires(cart.andEq("user_id", userId));
       cart.setRequires(cart.andEq("session_id", 1));
       List<NideshopCart> cartList = nideshopCartDao.selectNideshopCart(cart);
       for(NideshopCart c:cartList){
           goodsCount += c.getNumber();
           goodsAmount += c.getNumber() * c.getRetailPrice();
           if(null!=c.getChecked()){
               checkedGoodsCount += c.getNumber();
               checkedGoodsAmount += c.getNumber() * c.getRetailPrice();
           }
           // 查找商品的图片
           NideshopGoods good =nideshopGoodsDao.selectNideshopGoodsById(c.getGoodsId());
           c.setListPicUrl(good.getListPicUrl());
       }
       cartVo.setCartList(cartList);
       CartTotal cartTotal = new CartTotal();
       cartTotal.setCheckedGoodsAmount(checkedGoodsAmount);
       cartTotal.setCheckedGoodsCount(checkedGoodsCount);
       cartTotal.setGoodsAmount(goodsAmount);
       cartTotal.setGoodsCount(goodsCount);
       cartVo.setCartTotal(cartTotal);
       return cartVo;
   }



}
